using System;
using System.Xml;
using System.Collections;
using System.Runtime.InteropServices;

namespace ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Property
{
	//*********************************************************************
	//
	// Property Class
	//
	// The Property class represents the caracteristics of an attribute
	//
	//*********************************************************************
	public class XMLProperty : bOSdotNET.XML.Data.XMLObject
	{
		
		public const string MAIN_NODE = "Property";

		private const string TYPE_ATTRIBUTE		= "Type";
		private const string NAME_ATTRIBUTE		= "Name";
		private const string DESCRIPTION_NODE	= "Description";
		private const string DEFAULT_VALUE_NODE	= "DefaultValue";

		//add by mfarina on 27/04/2007 for resolving workaround 454
		private const string VALUE_ATTRIBUTE = "Value";

		/** FIELDS **/
		private string sType;
		private string sName;
		private string sDescription;
		private string sDefaultValue;

		private XMLPropertyValues vValues;

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Width simple Property **/
		public XMLProperty()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLProperty(System.Xml.XmlNode nodeList)
		{
			this.Parse (nodeList);
		}

		override public void Init()
		{
			sType= string.Empty;
			sName= string.Empty;
			sDescription= string.Empty;
			sDefaultValue = string.Empty;

			vValues = new XMLPropertyValues();
		}

		/** PROPERTIES **/
		public XMLPropertyValues XMLPropertyValues 
		{
			get { return vValues; }
			set { vValues = value; }
		}

		public string Name 
		{
			get { return sName; }
			set { sName = value; }
		}

		public string DefaultValue 
		{
			get { return sDefaultValue; }
			set { sDefaultValue = value; }
		}

		public string Type 
		{
			get { return sType; }
			set { sType = value; }
		}

		public string Description 
		{
			get { return sDescription; }
			set { sDescription = value; }
		}

		/** INSTANCE METHODS **/

		/** Parsering method for filling the property **/
		override public void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			/** find the attributes situated on the root tag **/
			for ( int i=0; i<nodeList.Attributes.Count;i++)
			{
				switch ( nodeList.Attributes.Item(i).Name ) 
				{
					case XMLProperty.NAME_ATTRIBUTE:
						sName = nodeList.Attributes.Item(i).InnerText;
						break;
					case XMLProperty.TYPE_ATTRIBUTE:
						sType = nodeList.Attributes.Item(i).InnerText;
						break;

					case XMLProperty.VALUE_ATTRIBUTE:
						vValues.Add ( new XMLPropertyValue ( nodeList.Attributes.Item(i).InnerText));
						break;

				}
			}

			/** find the sub tags witch contains propery values **/
			for ( int i=0; i<nodeList.ChildNodes.Count; i++)
			{
				node = nodeList.ChildNodes[i];

				switch (node.Name) 
				{
					case XMLProperty.DESCRIPTION_NODE:
						sDescription = node.InnerText;
						break;

					case XMLProperty.DEFAULT_VALUE_NODE:
						sDefaultValue = node.InnerText;
						break;

						/** define the subcollection of the propertyvalues By parsering the subtag Values **/
					case XMLPropertyValues.MAIN_NODE:
						vValues.Parse(node);
						break;
				} 
			}
		}

		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLProperty.MAIN_NODE);
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLProperty.NAME_ATTRIBUTE, sName) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLProperty.TYPE_ATTRIBUTE, sType) );

			if ( sDescription.CompareTo ( string.Empty ) != 0 ) 
				xmlNode.AddNode ( new bOSdotNET.XML.XMLNode (XMLProperty.DESCRIPTION_NODE, sDescription) );

			if ( sDefaultValue.CompareTo ( string.Empty ) != 0 ) 
				xmlNode.AddNode ( new bOSdotNET.XML.XMLNode (XMLProperty.DEFAULT_VALUE_NODE, sDefaultValue) );

			switch ( vValues.Count )
			{
				case 0:
					xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLProperty.VALUE_ATTRIBUTE, string.Empty) );
					break;
				case 1:
					XMLPropertyValue oneValue = (XMLPropertyValue)vValues[0];
					xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLProperty.VALUE_ATTRIBUTE, oneValue.Value) );
					break;

				default:
					xmlNode.AddNode ( vValues.GetXMLNode() );
					break;
			}
			
			return xmlNode;
		}
	}
}